<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 服务进度查看控制器
*/
class ServeproController extends Controller{
	public function index(){
		$shop_id = $_GET['shop_id'] ? $_GET['shop_id'] : 1;
		if ($shop_id) {
			$where['shop_id'] = $shop_id;
		}
		if($_GET['cate_id']){
			$where['cate_id'] = $_GET['cate_id'];
		}
		session('page_size',$_GET['page_size']);
		$page_no = I('get.p') ? I('get.p') : 1;
		$page_size = $_GET['page_size'] ? $_GET['page_size'] : 20;
		$data = M('device')->field("id as device_id,name,status")
		                   ->page($page_no, $page_size)
		                   ->where($where)
		                   ->select();

		$count = M('device')->where($where)->count();

		$where['status'] = 0;
		$not_count = M('device')->where($where)->count();
		$this->assign('count', $count);
		$this->assign('not_count', $not_count);
		$logModel = M('scan_log');
		foreach ($data as $key => $value) {
			$logRes = $logModel->field("max(add_time) as add_time,count(device_id) as num")->group('device_id')->where('device_id='.$value['device_id'])->select();
			if ($logRes) {
				$data[$key]['add_time'] = $logRes[0]['add_time'];
				$data[$key]['num'] = $logRes[0]['num'];
			}else{
				$data[$key]['add_time'] = 0;
				$data[$key]['num'] = 0;
			}
			
		}
		
		$Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        $shop = M('shop')->where('del=0')->select();
        // 分页显示输出
        $this->assign('type',$_SESSION['type']);
        $this->assign('shop', $shop);
        $this->assign('shop_id', $shop_id);
        $this->assign('page', $show);
        // 赋值分页输出
       	$this->assign("count", $count);
		$this->assign('data',$data);
        $this->display();
	}
}